<template>
  <div class="container page">
    <!-- <official-account></official-account> -->
    <p style="height:420rpx"></p>
    <!-- <button v-if='userinfo.openId' @click='jumpClick()' class='btn'>添加图书</button> -->
    <div class="logo">
      <!-- <p>logo</p>
      <p>人人测吧</p> -->
      <img src="https://www.rrjiaoyi.com/static/rr-min/me/login.png" mode="widthFix">
    </div>
    <button open-type="getUserInfo" lang="zh_CN" class='btn' @getuserinfo="login">微信授权登录</button>


  </div>
</template>
<script>
import qcloud from 'wafer2-client-sdk'
import YearProgress from '@/components/YearProgress'
import {showSuccess, showModal} from '@/util'
import {get, post} from '@/util'
import {get2, post2} from '@/server/api'
import config from '@/config'
export default {
  components: {
    YearProgress
  },
  data () {
    return {
      sharepath: '',
      openid_create: '',
      playerRank: [],
      userinfo: {
        avatarUrl: 'http://116.62.67.174:10009/stock/httpServiceImpl/image/2017-01-01/df_avatar.png',
        nickName: ''
      }
    }
  },
  mounted () {
    this.sharepath = this.$root.$mp.query.sharepath
    this.openid_create = this.$root.$mp.query.openid_create

    // 来自于分享邀请进群走登录
    if(this.openid_create) {
      return
    }
    // 走缓存登录
    if(wx.getStorageSync('userinfo')) {
      if (this.sharepath) {
        wx.switchTab({
          url: `/pages/${this.sharepath}/main`
        })
      } else {
        wx.switchTab({
          url: `/pages/home/main`
        })
      }
      // wx.switchTab({
      //   url: '/pages/home/main'
      // })
    } else {
      // login()
      return
    }

    // wx.showToast({
    //   title: '登录中',
    //   icon: 'loading'
    // })

    // qcloud.setLoginUrl(config.loginUrl)
    // const session = qcloud.Session.get()
    // console.log('session', session)
    // if(session) {
    //   this.loginSuccess(session.userinfo)
    //   wx.switchTab({
    //     url: '/pages/home/main'
    //   })
    // }

    // this.userinfo = wx.getStorageSync('userinfo')

    // this.bookid = this.$root.$mp.query.id

    // this.demo().then(result => {
    // console.log('demo', result.data);
    // });
    
    // this.joke().then(result => {
    // console.log('joke', result.data.data.msg);
    // });

    // this.player().then(result => {
    //   this.playerRank = result
    //   console.log('joke', result);
    // });

  },
  methods: {
    // 进群
    async createGroup(openid_create, openid_invited) {
      return await post2('create_group', {'openid_create': openid_create, 'openid_invited': openid_invited})
    },
    async player() {
      // return await post('minipro/List', {}, {'api':'all', 'page':1})
    },
    async demo() {
    // return await get2('demo', {})
    },
    async joke() {
      // return await get2('api/jokes', {})
    },
    jumpClick () {
      console.log('111111111111')
      wx.switchTab({
        url: '/pages/games/main'
        // url: '/pages/web-view/main?path=' + 'https://wj.qq.com/s/2491547/f457/'
        // url: '/pages/detail/main'
      })
      console.log('22222222222')
    },
    loginSuccess (res) {
      showSuccess('登录成功')
      wx.setStorageSync('userinfo', res)
      this.userinfo = res
    },
    // 登录
    login () {
      // wx.showToast({
      //   title: '登录中',
      //   icon: 'loading'
      // })
      // 显示加载图标
      wx.showLoading({
        title: '登录中'
      })

      // console.log('qcloud', qcloud)
      qcloud.setLoginUrl(config.loginUrl)
      // const session = qcloud.Session.get()
      // console.log('session', session)

      qcloud.login({
        success: res => {
 
          console.log('登录成功', res)
          this.loginSuccess(res)
          wx.hideLoading();

          console.log('openid_create', this.openid_create)
          // 来自于分享邀请进群
          if(this.openid_create) {
            console.log('进群')
            // console.log(this.openid_create)
            // console.log(wx.getStorageSync('userinfo').openId)
            this.createGroup(this.openid_create, wx.getStorageSync('userinfo').openId).then(result => {
              console.log('进群', result);
              // this.allInfo = result.result
            });
          }
          // console.log('sharepath',this.sharepath)
          if (this.sharepath) {
            wx.switchTab({
              url: `/pages/${this.sharepath}/main`
            })
          } else {
            wx.switchTab({
              url: `/pages/home/main`
            })
          }
          console.log('xxxxxxxxxx')
        },
        fail: err => {
          console.error(err)
          wx.hideLoading();
        }
      })

      // if (session) {

      //   // qcloud.loginWithCode({
      //   //   success: res => {
      //   //     console.log('没过期的登录', res)

      //   //2019.1.8
      //       this.loginSuccess(session.userinfo)
      //       console.log('zzzzzzzz')

      //       // wx.switchTab({
      //       //   url: '/pages/home/main'
      //       // })
      //       console.log('yyyyyyyyyyy')

      //   //   },
      //   //   fail: err => {
      //   //     console.error(err)
      //   //   }
      //   // })
      // } else {
      //   qcloud.login({
      //     success: res => {
      //       console.log('登录成功', res)
      //       this.loginSuccess(res)
      //       // wx.switchTab({
      //       //   url: '/pages/home/main'
      //       // })
      //       console.log('xxxxxxxxxx')
      //     },
      //     fail: err => {
      //       console.error(err)
      //     }
      //   })
      // }


    }

  },
  onShow () {
    // console.log(123)
    // let userinfo = wx.getStorageSync('userinfo')
    // console.log([userinfo])
    // if (userinfo) {
    //   this.userinfo = userinfo
    // }
    // console.log(this.userinfo)
  }
}
</script>

<style lang='scss'>
.container{
  font-size: 30rpx;
  color: white;
  // background-color: #070d1b;
  // width: 100%;
  // line-height: 100%;
  // opacity: 0.7;
}
page {
  background-color: #090f1d;
  // background-color: #070d1b;
  // background-repeat:no-repeat; background-size:100% 100%;-moz-background-size:100% 100%;
  // background-image: url("https://www.rrjiaoyi.com/static/rr-min/common/bg@3x.png");
  width: 100%;
  height: 100%;
}
.userinfo{
  // padding-top:100rpx;
  text-align:center;
  // margin-bottom: 100rpx;
  font-size: 20rpx;
  img{
    width: 150rpx;
    height:150rpx;
    margin: 10rpx;
    border-radius: 50%;
  }
}
.btn {
 background-color: rgb(62, 207, 221);
 width: 350rpx;
//  hover: black;
}
.button {
  hover-class: none;
}
.logo {
  text-align: center;
  margin-bottom: 50rpx;
  img {
    width: 40%;
  }
}
</style>
